/**
 * 
 */

function validateMenuFoodName() {
	var div = document.getElementById("error");
	while (div.hasChildNodes()) {
		div.removeChild(div.childNodes[0]);
	}
	var menuFoodName = document.forms["menufood"]["menuFoodName"].value;
	var textError = null;
	var check = true;
	if (menuFoodName == null || menuFoodName == "") {
		textError = "Menufood name must be filled out";
		addElements(div, textError);
		check = false;
	}
	var menuFoodNameBefore = $('#namebeforechange').val();
	if (menuFoodName != menuFoodNameBefore) {
		if (checkExistMenuFoodName() == true) {
			textError = "Menufood name has existed!";
			addElements(div, textError);
			check = false;
		}
	}

	return check;
}

function addElements(div, textError) {
	var dv = document.createElement("DIV");
	dv.setAttribute("class", "alert alert-danger");
	var sp1 = document.createElement("SPAN");
	sp1.setAttribute("class", "glyphicon glyphicon-exclamation-sign");
	dv.appendChild(sp1);
	var text = document.createTextNode(textError);
	dv.appendChild(text);
	div.appendChild(dv);
}

function checkExistMenuFoodName() {
	var flag = false;
	$.ajax({
		"type" : "POST",
		"async" : false,
		"url" : "/core-web-admin/menufood/isexisted",
		"data" : {
			"restaurantId" : $('#restaurantId').val(),
			"menuFoodName" : $('#menuFoodName').val()
		},
		"success" : function(data) {
			if (data == "true") {
				flag = true;
			}
			if (data == "false") {
				flag = false;
			}
		},
		"error" : function(data) {
			alert("ERROR WHEN CHECK EXISTED MENU FOOD NAME!");
			flag = false;
		}
	});
	return flag;
}

function updateMenuName() {
	$.ajax({
		"type" : "POST",
		"async" : false,
		"url" : "/core-web-admin/menufood/update",
		"data" : {
			"menuFoodId" : $('#foodId').val(),
			"menuFoodName" : $('#foodName').val()
		},
		"complete" : function() {
			location.reload();
		}
	});
}

function deleleMenuName(resId, mfId) {
	$.ajax({
		"type" : "POST",
		"async" : false,
		"url" : "/core-web-admin/menufood/delete",
		"data" : {
			"restaurantId" : resId,
			"menuFoodId" : mfId
		},
		"complete" : function() {
			window.location.reload();
		}
	});
}

function createFormUpdate() {
	$.ajax({
		"type" : "GET",
		"async" : false,
		"url" : "/core-web-admin/menufood/formupdate",
		"data" : {
			"menuFoodId" : 10,
			"menuFoodName" : "hello"
		}
	});
}

function backPage() {
	history.back();
}